package com.hj.javaweb.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hj.javaweb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author : hujing
 * @Date: 2025/6/20 15:37
 * @Description:
 */
@Service
public class JsonSerializationService {
    @Autowired
    private ObjectMapper objectMapper;
    
    //序列化对象为json字符串
    public String serialize(User user) throws JsonProcessingException {
        return objectMapper.writeValueAsString(user);
    }
    
    // 反序列化json字符串为对象
    public User deserialize(String json) throws JsonProcessingException {
        return objectMapper.readValue(json, User.class);
    }
}
